import { _decorator, Color, Component, EventMouse, Graphics, HorizontalTextAlignment, Label, LabelComponent, Layout, LayoutComponent, Node, Prefab, resources, Sprite, SpriteFrame, UITransform, VerticalTextAlignment, Widget } from 'cc';
import { NormalCellController } from './NormalCellController';
const { ccclass, property } = _decorator;

@ccclass('MonsterCellController')
export class MonsterCellController extends NormalCellController {
    @property({ type: Animation })
    public cellChangeAnimation: Animation | null = null;

    private hpMeters: Array<Node> = new Array;
    private hp: number;
    private lossHP: number;
    
    start() {

    }

    update(deltaTime: number) {

    }

    // 重新编写探索事件
    protected clickToNormalCell(event: EventMouse) {
        // if (this.isExplore) {
        //     // 该单元该以被探索
        //     return;
        // }

        // this.isExplore = true;

        // let node = event.currentTarget;

        // // 加载反转动画
        // // node.getComponent(Animation).play('DrawExploreX')

        // 定制化操作
        this.customClickToCell(event);
    }

    protected customClickToCell(event: EventMouse): void {
        if (this.explore) {
            return;
        }
        
        this.lossHP++;
        this.loadLossHPMeter();

        // 如果失去的血量和血量相同代表怪物已死亡代表已探索
        if (this.lossHP == this.hp) {
            this.explore = true;
            this.drawX(event);
        }
    }

    private drawX(event: EventMouse): void {
        // 画 X 
        let node = event.currentTarget;
        let graphics = node.getChildByName('ExploreX').getComponent(Graphics)

        let x = node.getComponent(UITransform).width - this.sizeX / 2 - 10;
        let y = node.getComponent(UITransform).height - this.sizeY / 2 - 10;

        graphics.moveTo(-x, -y);
        graphics.lineTo(x, y);
        graphics.moveTo(-x, y);
        graphics.lineTo(x, -y);
        graphics.stroke();
    }

    protected customCreatePrefab(item: Node, data: Object): void {
        this.hp = data['HP'];
        this.lossHP = data['LossHP'];

        // 重新设定字符
        let str = '';

        for (let num of this.value) {
            str += `${num}` + '/';
        }

        item.getChildByName('Value').getComponent(Label).string = str.substring(0, str.length - 1);
        item.getChildByName('Value').getComponent(Label).verticalAlign = VerticalTextAlignment.TOP;
        item.getChildByName('Value').getComponent(Label).horizontalAlign = HorizontalTextAlignment.CENTER;

        // 设定血量格子
        let hpNode = item.getChildByName('HP');
        let x = 0, y = 0;

        for (let i = 0; i < this.hp; i++, y -= 12) {
            if (Math.abs(y) > (this.sizeY - 20)) {
                y = 0;
                x -= 12;
            }

            this.hpMeters.push(this.createHPMeter(x, y));
            hpNode.addChild(this.hpMeters[this.hpMeters.length - 1]);
        }

        this.createDiamondBar(item, data);
    }

    private createDiamondBar(item: Node, data: Object): void {
        let diamondBar: Node = item.getChildByName("DiamondBar");
        let rewards: object = data['Reward'];
        let x = 0, y = 0;
        let lastDivisionLabelNode: Node = null;

        diamondBar.addComponent(Layout)
        diamondBar.getComponent(Layout).alignHorizontal = true
        diamondBar.getComponent(Layout).paddingLeft = 10;

        for (let reward of rewards['List']) {
            let object = this.createRewardNode(reward, x, y);
            let rewardNode: Node = object['node'];
            x = object['x'];
            y = object['y'];
            diamondBar.addChild(rewardNode);

            let divisionLabelNode = new Node();
            divisionLabelNode.setPosition(x, y);
            divisionLabelNode.addComponent(Label).string = "/";
            divisionLabelNode.getComponent(Label).fontSize = 12;
            divisionLabelNode.getComponent(Label).color = new Color(0, 0, 0, 255);
            diamondBar.addChild(divisionLabelNode);
            lastDivisionLabelNode = divisionLabelNode;
            x += 10;
        }

        diamondBar.removeChild(lastDivisionLabelNode);
    }

    private createRewardNode(data: object, x: number, y: number): object {
        let node: Node = new Node();
        let fontSize = 12;

        let diamondLabelNode = new Node();
        diamondLabelNode.setPosition(x, y);
        diamondLabelNode.addComponent(Label);
        diamondLabelNode.getComponent(Label).color = new Color(0, 0, 0, 255);
        diamondLabelNode.getComponent(Label).fontSize = fontSize;
        diamondLabelNode.getComponent(Label).string = data['Diamonds'];

        let diamondImageNode = new Node();
        diamondImageNode.setPosition(x += 12, y);
        diamondImageNode.addComponent(Sprite);
        resources.load("images/diamond/spriteFrame", SpriteFrame, (err, spriteFrame) => {
            diamondImageNode.getComponent(Sprite).spriteFrame = spriteFrame;
        });

        node.addChild(diamondLabelNode);
        node.addChild(diamondImageNode);

        if (data['HP'] != 0) {
            x += 12
            let hpLabelNode = new Node();
            hpLabelNode.setPosition(x, y);
            hpLabelNode.addComponent(Label);
            hpLabelNode.getComponent(Label).color = new Color(0, 0, 0, 255);
            hpLabelNode.getComponent(Label).fontSize = fontSize;
            hpLabelNode.getComponent(Label).string = data['HP'];

            let hpImageNode = new Node();
            hpImageNode.setPosition(x += 12, y);
            hpImageNode.addComponent(Sprite);
            resources.load("images/whiteBlock/spriteFrame", SpriteFrame, (err, spriteFrame) => {
                hpImageNode.getComponent(Sprite).spriteFrame = spriteFrame;
            });
            hpImageNode.getComponent(Sprite).color = new Color(200, 22, 29, 255)

            node.addChild(hpLabelNode);
            node.addChild(hpImageNode);
        }

        return {
            "node": node,
            "x": x += 10,
            "y": y
        };
    }

    private createHPMeter(x: number, y: number): Node {
        let hpMeterNode: Node = new Node();

        hpMeterNode.addComponent(UITransform);
        hpMeterNode.addComponent(Sprite);

        hpMeterNode.getComponent(UITransform).setContentSize(10, 10);
        hpMeterNode.setPosition(x, y);

        resources.load("images/checkBox/spriteFrame", SpriteFrame, (err, spriteFrame) => {
            hpMeterNode.getComponent(Sprite).spriteFrame = spriteFrame;
        });

        return hpMeterNode;
    }

    private loadLossHPMeter() {
        for (let i = 0; i < this.lossHP; i++) {
            resources.load("images/beCheckBox/spriteFrame", SpriteFrame, (err, spriteFrame) => {
                this.hpMeters[i].getComponent(Sprite).spriteFrame = spriteFrame;
            });
        }
    }

    protected customLoadPrefabCell(prefabPath: string, data: object): void {

    }
}


